package com.geline.mybatisplus.mapper;

import cn.hutool.core.bean.BeanUtil;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;

import java.util.List;
import java.util.Map;

/**
 * @author mengxin
 * @date 2025/4/18
 */
@Mapper
public interface CustomSqlMapper {

    //查询列表
    @Select(value = "${sql}")
    List<Map<String, Object>> selectMaps(@Param("sql") String sql);

    //分页查询列表
    @Select(value = "${sql} limit ${page.current}, ${page.size}")
    Page<Map<String, Object>> selectPageMaps(@Param("page") Page<?> page, @Param("sql") String sql);

    default <T> List<T> selectList(String sql, Class<T> itemClass) {
        List<Map<String, Object>> mapList = this.selectMaps(sql);
        return BeanUtil.copyToList(mapList, itemClass);
    }

    default <T> IPage<T> selectPage(Page<?> page, String sql, Class<T> itemClass) {
        Page<Map<String, Object>> mapPage = this.selectPageMaps(page, sql);
        List<T> list = BeanUtil.copyToList(mapPage.getRecords(), itemClass);
        Page<T> result = new Page<>(page.getCurrent(), page.getSize());
        result.setRecords(list);
        return result;
    }
}
